package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
)

const (
	base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)

var coder = base64.NewEncoding(base64Table)

// var coder = base64.NewDecoder(enc, r)

func base64Encode(src []byte) []byte {
	return []byte(coder.EncodeToString(src))
}

func base64Decode(src []byte) ([]byte, error) {
	return coder.DecodeString(string(src))
}

func main() {
	// encode
	hello := "kls一直小猫djf0126451"
	debyte := base64Encode([]byte(hello))
	fmt.Printf("%s\n", debyte)

	// decode
	enbyte, err := base64Decode(debyte)
	if err != nil {
		fmt.Println(err.Error())
	}

	if hello != string(enbyte) {
		fmt.Println("hello is not equal to enbyte")
	}

	fmt.Println(string(enbyte))

	var b bytes.Buffer
	w := base64.NewEncoder(base64.URLEncoding, &b)
	w.Write([]byte("kls一直小猫djf0126451"))
	w.Close()
	data := b.Bytes()
	fmt.Printf("%s\n", data)

	k := make([]byte, 2048)
	v := base64.NewDecoder(base64.URLEncoding, &b)
	v.Read(k)
	fmt.Printf("%s\n", k)
}
